<?php

namespace App\Common\Http\Controller;

use App\ApiBaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Handles\CrmRpcHandle;

class BannerController extends ApiBaseController
{
    use CrmRpcHandle;

    /**
     * 表单验证
     * @param $data
     * @param $type
     * @return bool
     * @throws GoodsException
     */
    protected function validate($data, $ruleArr, $source = '')
    {
        $rule = [
            'source' => 'required|in:1,2'
        ];

        $message = [
            'source.required' => '请传入投放源参数',
            'source.in' => '投放源参数异常'
        ];

        // 根据配置选择规则组成
        $rule = array_intersect_key($rule, array_flip($ruleArr));

        $validate = Validator::make($data, $rule, $message);
        if($validate->fails()){
            throw new \Exception($validate->errors()->first());
        }
        return true;
    }
    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/18 1:22 下午
     * Describe: 获取banner轮播图
     * Version: V1.0
     * @param Request $request
     */
    public function getList(Request $request) {
        // 接受参数
        $params = $request->input();

        $fields = ['source'];

        try {
            // 校验参数
            $this->validate($params, $fields);

            // 获取轮播图
            $data = $this->getBannerList($params['source']);

            return $this->success('获取成功', $data);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }
}
